<?php
    @include_once("conn.php");

    $key = $_GET["key"];
    $orderCol = $_GET["orderCol"];
    $pageNum = $_GET["pageNum"];
    $pageIndex = $_GET["pageIndex"];

    if(!($orderCol&&$pageNum&&$pageIndex)){
        argErr();
    }  

    $search = "select count(*) as allCount from `goodslist` where goodsName like '%$key%'";
    $result = mysqli_query($conn,$search);
    $item = mysqli_fetch_assoc($result);
    $allCount  = $item["allCount"];
    $maxPage = ceil($allCount/$pageNum);

    if($pageIndex>$maxPage){
        $pageIndex = $maxPage;
    }

    if($pageIndex<1){
        $pageIndex = 1;
    }

    $skipNum = ($pageIndex-1)*$pageNum;

    //goodsId, goodsName, goodsImg, goodsPrice
    $sql = "SELECT id,goodsId,goodsName,goodsPrice,bigPicList,smallPicList,goodsDetail from `goodslist` where goodsName like '%$key%' ORDER BY $orderCol LIMIT $skipNum,$pageNum";
    $result = mysqli_query($conn,$sql);
    //打印错误
    if (!$result) {
        printf("Error: %s\n", mysqli_error($conn));
        exit();
    }
    // $item = mysqli_fetch_assoc($result);
    $arr = array();
    while($item = mysqli_fetch_assoc($result)){
        $item["goodsId"] = $item["goodsId"]*1;
        $item["goodsName"] = $item["goodsName"];
        $item["bigPicList"] = explode(",",$item["bigPicList"]);
        $item["goodsImg"] =  $item["bigPicList"][0];
        $item["goodsPrice"] = $item["goodsPrice"]*1;

        array_push($arr,$item);
    }

    $obj = array();
    if(count($arr)>0){
        $obj["status"]=true;
        $obj["detail"]="ok";
        $obj["maxPage"]=$maxPage;
        $obj["allCount"]=$allCount;
        $obj["list"]=$arr;
    }else{
        $obj["status"]=false;
        $obj["detail"]="暂无数据";
    }

    echo json_encode($obj);
?>